/**
 * [OElove] (C)2010-2099 OELOVE.COM Inc. X
 * Email: service@phpcoo.com ,phpcoo@qq.com
 * This is NOT a freeware, use is subject to license terms
 * $ Last update 2016/12/08 by OE $
*/
$(function(){ 

	//显示扫码
	$(document).on("click", "[f='but_show_scan']", function(){
		if ($(this).find("span").css("display") == "none") {
			$(this).find("span").show();
		}
		else {
			$(this).find("span").hide();
		}
	});
	
	//打开上传相册页面
	$(document).on("click", "[f='but_open_addphoto']", function(){
		$remain_photos =  parseInt($("#remain_photos").val());
		if (typeof($remain_photos) == "undefined") {
			$remain_photos = 0;
		}
		if ($remain_photos < 1) {
			ToastShow("对不起，可上传相册张数已满！");
			return false;
		}
		$_type = $(this).attr("data-type");
		if (typeof($_type) == "undefined") {
			$_type = 0;
		}
		$ajax_url = _OE_FILE + "&c=photo&a=addto&type="+$_type;
		oeShadeBox("addto_photo", "ajax", $ajax_url, 600, 450);
	});


	//上传照片
	$(document).on("change", "#imgfile_photo", function(){
		$remain_photos =  parseInt($("#remain_photos").val()); //可上传数量
		if (typeof($remain_photos) == "undefined") {
			$remain_photos = 0;
		}

		$file_num = parseInt($("#file_num").val()); //当前第几张
		if (typeof($file_num) == "undefined") {
			$file_num = 1;
		}
		$photo_type = $(this).attr("data-type");
		if (typeof($photo_type) == "undefined") {
			$photo_type = 0;
		}
		//创建Li
		$append_li = "<dl id='img_"+$file_num+"'><dt></dt><dd><img src='"+_ROOT_PATH+"tpl/user/images/loading_s.gif' /></dd></dl>";
		$("#addto_photo_list").append($append_li);

		//上传照片
		$.ajaxFileUpload({
			"url": _ROOT_PATH+"index.php?m=user&c=photo&a=saveadd&phototype="+$photo_type,
			"secureuri": false,
			"fileElementId": $(this).attr("id"),
			"dataType": "json",
			"data": {uploadpart: $(this).attr("id")},
			async: this.async,
			beforeSend: function ($xmlobj) {},
			complete: function ($data) {},
			success: function ($data) {
				$json = eval($data);
				$response = $json.response;
				$result = $json.result;
				$photoid = $json.photoid;
				$attid = $json.attid;
				if (typeof($attid) == "undefined") {
					$attid = "";
				}
				if ($response == "1") { //上传成功
					$_html = "<img src='"+$result+"' /><img src='"+_ROOT_PATH+"tpl/user/images/img_line.png' class='line' />";
					$("#img_"+$file_num+" dt").html($_html);
					$("#img_"+$file_num+" dd").html("");
					$("#file_num").val($file_num+1);//下一张
					$("#photoid").val($("#photoid").val()+$photoid+",");

					if ($file_num >= $remain_photos) {
						$("#upload_file_li").remove();
					}

					_oeloveFileSynYun($attid); //同步云存储
				}
				//失败
				else {
					$error = "上传失败，请检查照片大小和格式！";
					if ($result.length > 0 ) {
						$error = $result;
					}
					ToastShow($error);
					$("#img_"+$file_num).remove();
				}

			},
			error: function ($data, e, b) {
				ToastShow("上传失败，请检查照片大小和格式！");
				$("#img_"+$file_num).remove();

			}
		});

	});

	//确定发布_同步到动态
	$(document).on("click", "[f='but_photo_confirm_dynamic']", function(){
		$photoid = $("#photoid").val();
		$photo_type = $(this).attr("data-type");
		$.ajax({
			type: "POST",
			url: _ROOT_PATH+"index.php?m=user&c=photo",
			cache: false,
			data: {a:"dynamic", photoid:$photoid, photo_type:$photo_type, r:get_rndnum(8)},
			dataType: "json",
			success: function($data) {
				$json = eval($data);
				$response = $json.response;
				$result = $json.result;
				if ($response == "1") {
					window.location.reload();
				}
				else {
					if ($result.length > 0) {
						ToastShow($result);
					}
					else {
						ToastShow("操作失败");
					}
				}
			},
			error: function() {
				ToastShow("系统繁忙，请稍后再试");
			}
		});
		
	});


	//显示隐藏 操作项
	$(document).on("mouseover", "[f='but_show_photo_hand']", function(){
		if ($(this).find("dd").css("display") == "none") {
			$(this).find("dd").show();
		}
	});
	$(document).on("mouseout", "[f='but_show_photo_hand']", function(){
		if ($(this).find("dd").css("display") == "block") {
			$(this).find("dd").hide();
		}
	});

	
	//确定删除
	$(document).on("click", "[f='but_photo_del']", function(){
		$_id = $(this).attr("data-id");
		$_url = _OE_FILE+"&c=photo&a=del&id="+$_id;
		oeAjaxConfirmBox($_url, "确定要删除吗？", function(){
			window.location.reload();
		});
	});
	//设置公开、私密
	$(document).on("click", "[f='but_photo_set']", function(){
		$_id = $(this).attr("data-id");
		$_mod = $(this).attr("data-mod");

		if ($_mod == "public") {
			$_url = _OE_FILE+"&c=photo&a=setpublic&id="+$_id;
			oeAjaxConfirmBox($_url, "确定要设置为公开吗？", function(){
				window.location.reload();
			});
		}
		else {
			$_url = _OE_FILE+"&c=photo&a=setprivate&id="+$_id;
			oeAjaxConfirmBox($_url, "确定要设置为私密吗？", function(){
				window.location.reload();
			});
		}
		
	});

});


